package com.netafull;

import com.netafull.util.CloseUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;

public class NewNews extends ShowAdActivity {

	ImageButton btn_back;
	WebView webView;
	String url;

	View progress;

	Button mNewNews_back;
	Button mNewNews_go;
	
	boolean b = false;
	
	SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newnews);
		
		show();
		
		sp = getSharedPreferences("netafull",Context.MODE_PRIVATE);

		CloseUtil.activityList.add(NewNews.this);
		
		Intent intent = getIntent();
		url = intent.getExtras().getString("url");
		//url = "http://www.baidu.com";
		progress = findViewById(R.id.list_progress);

		mNewNews_back = (Button) findViewById(R.id.newnews_back);
		mNewNews_go = (Button) findViewById(R.id.newnews_go);
		btn_back = (ImageButton) findViewById(R.id.newnews_button_left);
		webView = (WebView) findViewById(R.id.newnews_webview);
		webView.setClickable(true);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(webView.canGoBack()){
					webView.goBack();
				} else {
					NewNews.this.finish();
					webView.clearCache(true);
				}
				
				
			}
		});

		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				progress.setVisibility(View.GONE);
				webView.setVisibility(View.VISIBLE);
				System.out.println("开始请求完成");
				if (view.canGoBack()) {
					mNewNews_back.setTextColor(Color.argb(150, 255, 255, 255));
				} else {
					mNewNews_back.setTextColor(Color.argb(50, 255, 255, 255));
				}

				if (view.canGoForward()) {
					mNewNews_go.setTextColor(Color.argb(150, 255, 255, 255));
				} else {
					mNewNews_go.setTextColor(Color.argb(50, 255, 255, 255));
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				progress.setVisibility(View.VISIBLE);
				System.out.println("开始请求" + url);
			}

		});
		mNewNews_back.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (webView.canGoBack()) {
					webView.goBack();
				}
			}

		});
		mNewNews_go.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (webView.canGoForward()) {
					webView.goForward();
				}
			}

		});
		LoadUrl();
	}

	private void LoadUrl() {
		// myDialog = ProgressDialog.show(NewNews.this, "",
		// getResources().getString(R.string.loading), true);
		progress.setVisibility(View.VISIBLE);
		webView.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				try {
					webView.loadUrl(url);

				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}.start();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(webView.canGoBack()){
				webView.goBack();
			}else {
				NewNews.this.finish();
				webView.clearCache(true);
			}
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onUserLeaveHint() {
		// TODO Auto-generated method stub
		super.onUserLeaveHint();
		if(!b){
			if(sp!=null){
				Editor editor = sp.edit();
				editor.clear();
				editor.commit();
			}
			System.out.println("开始销毁Activity");
			CloseUtil.closeActivity();
		}else{
			System.out.println("没有销毁Activity");
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		b = false;
	}

}
